//
//  ScrollBar.m
//  insbuy
//
//  Created by 任瑞飞任 on 15/9/20.
//  Copyright (c) 2015年 mobisoft. All rights reserved.
//

#import "ScrollBar.h"

@interface ScrollBar ()
{
    UILabel* _scrollbar;///滚动条
    SVButton* _currentdockbtn;///当前选中的dockbutton
    NSMutableArray *_btnArrays;
}
@end
@implementation ScrollBar
-(instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    if (self) {
    }
    return self;
}
-(void)setHeaderData
{
    [self UpdataUI];
}
-(void)setCurrentSelect:(NSInteger)currentSelect{
    _currentSelect = currentSelect;
    
    SVButton *btn =(SVButton *)[self viewWithTag:currentSelect+1000];
    [self DockClick:btn];
}
-(void)UpdataUI{
    
    [self setBackgroundColor:WHITE_COLOR];
    CGFloat width = kMainScreen_Width/_btnArray.count;
    CGFloat height =H(self) - 2;
    _btnArrays=[NSMutableArray array];
    
    for (int i = 0; i<_btnArray.count; i++) {
        
        SVButton* btn = [[SVButton alloc]initWithFrame:CGRectMake(width*(i%_btnArray.count), 0, width, height)];
       
        btn.tag = i+1000;
        [btn setDataWith:_btnArray[i] and:_numArray[i]];
        [btn addTarget:self action:@selector(DockClick:) forControlEvents:UIControlEventTouchUpInside];
        btn.backgroundColor = self.backgroundColor;
        [self addSubview:btn];
        if (i ==0) {
            [self DockClick:btn];
        }
        [_btnArrays addObject:btn];
    }
    
    _scrollbar = [[UILabel alloc]initWithFrame:CGRectMake(0, H(self)-2, width, 2)];    
    if (!isNULL(self.barColor)) {
        _scrollbar.backgroundColor = self.barColor;
    }else if(!isNULL(self.selectColor)){
        _scrollbar.backgroundColor = self.selectColor;
    }else{
        _scrollbar.backgroundColor = MAIN_COLOR;
    }
    [self laysetX:_scrollbar andRadius:width*_currentSelect];
    
    [self addSubview:_scrollbar];
}
-(void)reloadHeaderData
{
    for (int i = 0; i<_btnArrays.count; i++) {
        SVButton* btn = _btnArrays[i];
        [btn btnReloadNum:_numArray[i]];
    }
}
-(void)DockClick:(SVButton*)sender
{
    if (!isNULL(self.unSelectColor)) {
        [_currentdockbtn setSelected:NO];
    }else{
        [_currentdockbtn setSelected:NO];
    }
    
    _currentdockbtn = sender;
   
    if (!isNULL(self.selectColor)) {
        [_currentdockbtn setSelected:YES];
    }else{
        [_currentdockbtn setSelected:NO];
    }
    
    if ([self.delegate respondsToSelector:@selector(ScrollBar:andBtutton:)]) {
        [self.delegate ScrollBar:self andBtutton:sender];
    }
    if ([self.delegate respondsToSelector:@selector(ScrollBar:andBtutton:andIndex:)]) {
        [self.delegate ScrollBar:self andBtutton:sender andIndex:sender.tag-1000];
    }
  
    [UIView animateWithDuration:.8 animations:^{
        [self laysetX:_scrollbar andRadius:W(sender)*(sender.tag-1000)];
        
    } completion:^(BOOL finished) {
        
    }];
}
-(void)laysetX:(UIView*)sender andRadius:(CGFloat)x{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        
        dispatch_async(dispatch_get_main_queue(), ^{
            CGRect fram=sender.frame;
            fram.origin.x=x;
            sender.frame=fram;
        });
    });
    
}

@end
